﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Diagnostics;
using System.Configuration;
using System.IO;
using System.Windows.Forms;

namespace AuroraLightsFX
{
    public class LogManager
    {
        public enum LogLevel
        {
            Info,
            Warning,
            Fail
        }

 

 

        // Fields
        private static string LogFileName;
        private static bool LogginEnabled;

        // Methods
        public void DisableLog()
        {
            LogginEnabled = false;
        }

        public static void EnableLog()
        {
            LogginEnabled = true;
        }

        public static void Indent()
        {
            Debug.Indent();
        }

        public static void Initialize(string logFileName)
        {
            
            string str = (ConfigurationManager.AppSettings["LogFilePath"] != null) ? ConfigurationManager.AppSettings["LogFilePath"].ToString() : "";
            if (str == string.Empty)
            {
                str = Application.StartupPath + @"\";
            }
            else
            {
                if (str.Substring(0, 1) == @"\")
                {
                    str = str.Substring(1);
                }
                if (str.Substring(str.Length - 1, 1) != @"\")
                {
                    str = str + @"\";
                }
            }
            string str2 = DateTime.Now.ToString("-yyyy-MM-dd") + ".log";
            LogFileName = str + logFileName + str2;
            if (Path.GetPathRoot(LogFileName).IndexOf(":") < 0)
            {
                LogFileName = Path.GetDirectoryName(Application.StartupPath) + @"\" + LogFileName;
            }
            string directoryName = Path.GetDirectoryName(LogFileName);
            if (!Directory.Exists(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }
            TextWriterTraceListener listener = new TextWriterTraceListener(File.Open(LogFileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite));
            Trace.Listeners.Add(listener);
        }

        private static bool IsLogginValid()
        {
            return (((LogFileName != null) && (LogFileName.Length > 0)) && (Debug.Listeners.Count > 0));
        }

        public static void Log(LogLevel level, string category, string note)
        {
            if (IsLogginValid())
            {
                try
                {
                    Debug.WriteLine(note, string.Format("{0} Level:{1}, Category:{2} ", LogTime, level.ToString(), (category.Trim().Length == 0) ? "None" : category));
                    Trace.Flush();
                }
                catch
                {
                }
            }
        }

        public static void LogException(Exception ex)
        {
            if (IsLogginValid() && LogginEnabled)
            {
                Log(LogLevel.Fail, ex.TargetSite.Name, ex.Message);
                Indent();
                Debug.WriteLine(ex.InnerException, "InnerException:");
                Debug.WriteLine(ex.StackTrace, "StackTrace:");
                Unindent();
            }
        }

        public static void LogIf(bool condition, LogLevel level, string category, string note)
        {
            if (IsLogginValid() && condition)
            {
                Log(level, category, note);
            }
        }

        public void NewLine()
        {
            Debug.WriteLine("");
        }

        public static void Unindent()
        {
            Debug.Unindent();
        }

        // Properties
        public static string LogTime
        {
            get
            {
                return DateTime.Now.ToString("MM-dd-hh:mm:ss");
            }
        }
    }

 

}
